package attack.nartan;

import java.awt.geom.Rectangle2D;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.geom.Point;
import org.newdawn.slick.geom.Rectangle;

import framework.BasicFighter;
import framework.attack.BasicAttack;
import framework.movements.LinearMovement;

public class DownStrike extends BasicAttack<BasicFighter> {

	protected Image upForward;

	protected Image downForward;

	protected Image upBackward;
	
	protected Image downBackward;

	protected Image inUseUp;
	protected Image inUseDown;

	public DownStrike(BasicFighter fighter) {
		setAttacker(fighter);
		this.upForward = fighter.getAnimations().getSpriteSheet().getSprite(4, 4);
		this.downForward = fighter.getAnimations().getSpriteSheet().getSprite(0, 5);
		this.upBackward = this.upForward.getFlippedCopy(true, false);
		this.downBackward = this.downForward.getFlippedCopy(true, false);
	}

	@Override
	public void trigger() {
		if (getAttacker().getY() <= getAttacker().getHitBox().getHeight() * 2) {
			super.trigger();
			getAttacker().interruptJump();
			setYspeed(1800);
			if (getAttacker().isForward()) {
				this.inUseUp = this.upForward;
				this.inUseDown = this.downForward;
			} else {
				this.inUseUp = this.upBackward;
				this.inUseDown = this.downBackward;
			}
		}
	}

	@Override
	public void draw(Graphics g) {
		BasicFighter a = getAttacker();
		this.inUseUp.draw(a.getX(), a.getY(), a.getWidth(), a.getHeight());
		this.inUseDown.draw(a.getX(), a.getY() - 8 * getScale() + a.getHeight(), a.getWidth(), a.getHeight());
	}

	@Override
	public void onHit(BasicFighter f) {
		super.onHit(f);
	}

	@Override
	public void onHit(BasicFighter f, Rectangle2D Collision) {
		BasicFighter a = getAttacker();
		int forward = a.isForward() ? 1 : -1;
		if (!hasHit) {
			LinearMovement m = new LinearMovement(a, new Point(a.getX() - (a.getHeight() / 1.5f * forward), a.getY() - a.getHeight() / 2));
			m.setDuration(250);
			a.move(m);
			if (getHitBox().getY() < f.getHitBox().getY()) {
				super.onHit(f, Collision);
				interrupt();
				a.interruptJump();
			}

		}
	}

	@Override
	public boolean isDrawingAttacker() {
		return super.isActive();
	}

	@Override
	public int getDuration() {
		return 10000;
	}

	@Override
	public boolean isBlocking() {
		return isActive() && currentTime < 120;
	}

	@Override
	public void update(GameContainer gc, int delta) {
		super.update(gc, delta);
		if (!isBlocking()) {
			getAttacker().setY(getAttacker().getY() + getStepYSpeed(delta));
		}
		if (getAttacker().getYspeed() == 0) {
			super.interrupt();
		}
	}

	@Override
	public Rectangle getHitBox() {
		Rectangle r = getAttacker().getHitBox();
		int height = (int) (r.getHeight() / 4);
		return new Rectangle(r.getX() - r.getWidth() / 2, 
				r.getY() + r.getHeight() - height * 2, r.getWidth() * 2, height);
	}
}
